package com.uflower.order.controller;

import com.uflower.order.service.CartService;
import com.uflower.order.vo.CartItemVo;
import com.uflower.order.vo.CartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Api(value = "购物车Controller",tags="购物车页面的CRUD")
@Controller
@RequestMapping("order/cart")
public class CartController {
    @Autowired
    CartService cartService;

    /**
     * 显示购物车页面
     * @param model
     * @return
     */
    @ApiOperation(value = "显示用户的购物车界面")
    @ApiImplicitParam(name = "model",value = "向前端传购物车数据",dataType = "Model")
    @GetMapping(value = "/cartList.html")
    public String cartListPage(Model model){
        CartVo cartVo = cartService.getCart();
        model.addAttribute("cart", cartVo);
        return "cartList";
    }

    /**
     * 将商品加入购物车
     * @param skuId 商品号
     * @param num  商品数量
     * @param redirect
     * @return
     */
    @ApiOperation(value = "将商品加入购物车并跳转至成功页面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuId", value = "商品skuId", dataType = "Long"),
            @ApiImplicitParam(name = "num", value = "商品数量", dataType = "Integer")
    })
    //  http://localhost:88/api/order/cart/addCartItem?skuId=1&num=3
    @GetMapping("/addCartItem")
    public String addCartItem(@RequestParam("skuId") Long skuId,
                              @RequestParam("num") Integer num,
                              RedirectAttributes redirect){
        try {
            CartItemVo cartItemVo = cartService.addCartItem(skuId,num);
            redirect.addAttribute("skuId",skuId);
            return "redirect:http://localhost:88/api/order/cart/addCartItemSuccessPage.html";
        }catch (Exception e){
            e.printStackTrace();
            return "success";
        }

    }
    @GetMapping("/addCartItemSuccessPage.html")
    public String addCartItemSuccessPage(@RequestParam("skuId") Long skuId,
                                         Model model){
        CartItemVo cartItemVo = cartService.getCartItem(skuId);
        model.addAttribute("cartItem",cartItemVo);
        return "success";
    }

    /**
     * 更改购物车选中状态
     * @param skuId
     * @param checked
     * @return
     */
    @ApiOperation(value = "更改商品选中状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuId", value = "商品skuId", dataType = "Long"),
            @ApiImplicitParam(name = "checked", value = "是否选中(1选中,0没选中)", dataType = "Integer")
    })
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("checked") Integer checked){
        cartService.checkItem(skuId,checked);
        return "redirect:http://localhost:88/api/order/cart/cartList.html";
    }

    /**
     * 更改商品数量
     * @param skuId
     * @param num
     * @return
     */
    @ApiOperation(value = "更改商品数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuId", value = "商品skuId", dataType = "Long"),
            @ApiImplicitParam(name = "num", value = "商品数量", dataType = "Integer")
    })
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num){
        cartService.updateItemCount(skuId,num);
        return "redirect:http://localhost:88/api/order/cart/cartList.html";
    }


    /**
     * 删除购物项
     * @param skuId
     * @return
     */
    @ApiOperation(value = "删除购物项")
    @ApiImplicitParam(name = "skuId", value = "商品skuId", dataType = "Long")

    @GetMapping(value = "/deleteItem")
    public String deleteItem(@RequestParam("skuId") List<Long> skuId) {
        for (Long id : skuId) {
            cartService.deleteItem(id);
        }
        return "redirect:http://localhost:88/api/order/cart/cartList.html";
    }

}
